package com.tensquare.friend.service;

import com.tensquare.friend.dao.FriendDao;
import com.tensquare.friend.pojo.Friend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional
public class FriendService {
    @Autowired
    private FriendDao friendDao;
    //1.添加好友
    public int add(String id,String friendid){
        //1.1 根据userid和friendid查询是有添加过用户
        int count = friendDao.selectCount(id,friendid);
        System.out.println("count:" + count);
        //1.2 如果有记录就返回
        if (count > 0) return 0;
        //1.2 添加好友
        Friend friend = new Friend(id,friendid);
        friend.setIslike("0");    //单方添加，为0
        friendDao.save(friend);
        //1.4 查询是否有对方添加用户
        int count1 = friendDao.selectCount(friendid,id);
        if (count1 >  0){  //如果对方添加过用户，更改islike为1
            friendDao.updateIsLike("1",id,friendid);
            friendDao.updateIsLike("1",friendid,id);

        }
        return 1;
    }
    //2. 删除好友
    public void dele(String id, String friendid) {
        friendDao.deleteByUseridAndFriend(id,friendid);
    }
}
